import { Cartesian3, EllipsoidGeodesic, Viewer } from "cesium";
import { GCS, MathUtils } from "../CustomCesium/CustomCesium";

/**
 * description: 测量计算
 * fileName: MeasuringUtils.ts
 * author: ssr
 * date: 2023-01-13 09:19:39
 * version: V1.0.0
 */
export default class MeasuringUtils {
  /**
   * 笛卡尔坐标数组转换为经纬度数组
   */
  static cartesianArrayToWGS84Array(
    viewer: Viewer,
    cartesianArr: Cartesian3[]
  ): GCS[] {
    return cartesianArr
      ? cartesianArr.map(function (item) {
          return MathUtils.car3ToGCS(viewer, item);
        })
      : [];
  }

  /**
   * 累计获取84坐标的距离
   * @param {*} positions
   */
  static getPositionsDistance(positions: GCS[]): number {
    let distance = 0;
    for (let i = 0; i < positions.length - 1; i++) {
      let point1cartographic = MathUtils.gcsToCartographic(positions[i]);
      let point2cartographic = MathUtils.gcsToCartographic(positions[i + 1]);
      let geodesic = new EllipsoidGeodesic();
      geodesic.setEndPoints(point1cartographic, point2cartographic);
      let s = geodesic.surfaceDistance;
      s = Math.sqrt(
        Math.pow(s, 2) +
          Math.pow(point2cartographic.height - point1cartographic.height, 2)
      );
      distance = distance + s;
    }
    return distance;
  }
}
